<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue学习使用</title>
  <!-- 第1步： 引入vue库文件 -->
  <script src="./js/vue.js"></script>
</head>

<body>
  <!-- 第2步：挂载点 -->
  <div id="app">
    <div>
      <input type="text" v-validate.phone v-model="phone">
    </div>
    <div>
      <input type="text" v-validate.email v-model="email">
    </div>
    <div>
      <input type="text" v-validate.str="{len:3,msg:'长度过短'}" v-model="str">
    </div>
  </div>

  <!-- 第3步：实例化vue -->
  <script>
    const validateMethod = {
      phone(el) {
        // 手机号码
        let reg = /^1[3-9]\d{9}$/
        if (!reg.test(el.value)) {
          // 不合法
          el.style.color = 'red'
          if (!el.nextSibling) {
            const spanDom = document.createElement('span')
            spanDom.innerHTML = '不合法,修改一下'
            // el.parentNode?.appendChild(spanDom)
            el.parentNode && el.parentNode.appendChild(spanDom)
          }
        } else {
          el.style.color = 'black'
          el.nextSibling && el.nextSibling.remove()
        }
        console.log('phone');
      },

      email(el, value) {
        console.log('email')
      },
      str(el, value) {
        if (value) {
          if (el.value.length > value.len) {
            if (!el.nextSibling) {
              const spanDom = document.createElement('span')
              spanDom.innerHTML = value.msg
              el.parentNode?.appendChild(spanDom)
            }
          } else {
            el.nextSibling?.remove()
          }
        }
      }
    }

    Vue.directive('validate', (el, { value, modifiers }) => {

      Object.keys(modifiers).forEach(name => {
        validateMethod[name](el, value)
      })
    })

    const vm1 = new Vue({
      el: '#app',
      data: {
        phone: '13525125121',
        email: 'aa@aa.com',
        str: 'aaa'
      },
      methods: {
      }
    })
  </script>

</body>

</html>